package com.liuzhongni.wahahanews.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.liuzhongni.wahahanews.R;
//import com.liuzhongni.wahahanews.headlineclass.MyMemoryCache;
import com.liuzhongni.wahahanews.headlineclass.special.SpacialFirst;
import com.liuzhongni.wahahanews.headlineclass.special.SpacialFourth;
import com.liuzhongni.wahahanews.headlineclass.special.SpacialSecond;
import com.liuzhongni.wahahanews.singleton.VolleySingleton;

import java.util.ArrayList;

/**
 * Created by admin on 2016/1/11.
 */
public class ListViewAdapterOfSpecial extends BaseExpandableListAdapter {
    private SpacialSecond spacialSecond;
    private SpacialFirst SpacialFirst;
    private ArrayList<ArrayList<SpacialFourth>> fourths;
    private Context context;
//    private MyMemoryCache cache = new MyMemoryCache();

    public ListViewAdapterOfSpecial(SpacialSecond spacialSecond, Context context) {
        this.spacialSecond = spacialSecond;
        this.context = context;
    }

//    private void init() {
//        titleData = new ArrayList<>();
//        int num = spacialFirst.getSpacial().get(0).getTopics().size();
//        for (int i = 0; i <num; i++) {
//            String title= spacialFirst.getSpacial().get(0).getTopics().get(i).getTname();
//            titleData.add(title);
//        }
//    }


    @Override
    public int getGroupCount() {
        int num = spacialSecond.getTopics().size();
        return num;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return spacialSecond.getTopics().get(groupPosition).getDocs().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return spacialSecond.getTopics().get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return spacialSecond.getTopics().get(groupPosition).getDocs().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView==null){
            convertView = LayoutInflater.from(context).inflate(R.layout.threeyears_group_item,null);
            groupViewHolder = new GroupViewHolder(convertView);
            convertView.setTag(groupViewHolder);
        }else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }

        groupViewHolder.textViewGroupIndex.setText((groupPosition+1)+"/"+spacialSecond.getTopics().size());
        groupViewHolder.textViewGroupTitle.setText(spacialSecond.getTopics().get(groupPosition).getTname());

        return convertView;
    }

    @Override
    public int getChildTypeCount() {
        return 2;
    }

    @Override
    public int getChildType(int groupPosition, int childPosition) {
        return super.getChildType(groupPosition, childPosition);
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolderType0 holderType0;
        ChildViewHolderType1 holderType1;
        View convertView0 = null;
        View convertView1 = null;
        if(convertView0==null||convertView1==null){
            if(spacialSecond.getTopics().get(groupPosition).getDocs().get(childPosition).getImgextra()==null){
                convertView0 = LayoutInflater.from(context).inflate(R.layout.threeyears_child_item_type0,null);
                holderType0 = new ChildViewHolderType0(convertView0);
                convertView0.setTag(holderType0);

                VolleySingleton.getOurInstance().imgJson(holderType0.networkImageViewChildType0, spacialSecond.getTopics().get(groupPosition).getDocs().get(childPosition).getImgsrc());
                holderType0.textViewChildType0Title.setText(spacialSecond.getTopics().get(groupPosition).getDocs().get(childPosition).getTitle());
                holderType0.textViewChildType0Digest.setText(spacialSecond.getTopics().get(groupPosition).getDocs().get(childPosition).getDigest());
                holderType0.textViewChildType0ReplyCount.setText(spacialSecond.getTopics().get(groupPosition).getDocs().get(childPosition).getReplyCount()+"");
                return convertView0;
            }else {
                convertView1 = LayoutInflater.from(context).inflate(R.layout.threeyears_child_item_type1,null);
                holderType1 = new ChildViewHolderType1(convertView1);
                convertView1.setTag(holderType1);

                holderType1.textViewChildType1Title.setText(spacialSecond.getTopics().get(groupPosition).getDocs().get(childPosition).getTitle());
                VolleySingleton.getOurInstance().imgJson(holderType1.networkImageViewChildType1First, spacialSecond.getTopics().get(groupPosition).getDocs().get(childPosition).getImgsrc());
                VolleySingleton.getOurInstance().imgJson(holderType1.networkImageViewChildType1Second, spacialSecond.getTopics().get(groupPosition).getDocs().get(childPosition).getImgextra().get(0).getImgsrc());
                VolleySingleton.getOurInstance().imgJson(holderType1.networkImageViewChildType1Third, spacialSecond.getTopics().get(groupPosition).getDocs().get(childPosition).getImgextra().get(1).getImgsrc());
                return convertView1;

            }

        }else {
            if(spacialSecond.getTopics().get(groupPosition).getDocs().get(childPosition).getImgextra()==null){
                holderType0 = (ChildViewHolderType0) convertView0.getTag();
                VolleySingleton.getOurInstance().imgJson(holderType0.networkImageViewChildType0, spacialSecond.getTopics().get(groupPosition).getDocs().get(childPosition).getImgsrc());
                holderType0.textViewChildType0Title.setText(spacialSecond.getTopics().get(groupPosition).getDocs().get(childPosition).getTitle());
                holderType0.textViewChildType0Digest.setText(spacialSecond.getTopics().get(groupPosition).getDocs().get(childPosition).getDigest());
                holderType0.textViewChildType0ReplyCount.setText(spacialSecond.getTopics().get(groupPosition).getDocs().get(childPosition).getReplyCount()+"");
            }else {
                holderType1 = (ChildViewHolderType1) convertView1.getTag();
                holderType1.textViewChildType1Title.setText(spacialSecond.getTopics().get(groupPosition).getDocs().get(childPosition).getTitle());
                VolleySingleton.getOurInstance().imgJson(holderType1.networkImageViewChildType1First, spacialSecond.getTopics().get(groupPosition).getDocs().get(childPosition).getImgsrc());
                VolleySingleton.getOurInstance().imgJson(holderType1.networkImageViewChildType1Second, spacialSecond.getTopics().get(groupPosition).getDocs().get(childPosition).getImgextra().get(0).getImgsrc());
                VolleySingleton.getOurInstance().imgJson(holderType1.networkImageViewChildType1Third, spacialSecond.getTopics().get(groupPosition).getDocs().get(childPosition).getImgextra().get(1).getImgsrc());
            }
        }
        return null;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    public class GroupViewHolder{
        private TextView textViewGroupIndex;
        private TextView textViewGroupTitle;

        public GroupViewHolder(View view) {
            textViewGroupIndex = (TextView) view.findViewById(R.id.group_item_index);
            textViewGroupTitle = (TextView) view.findViewById(R.id.group_item_title);
        }
    }

    public class ChildViewHolderType0{
        private NetworkImageView networkImageViewChildType0;
        private TextView textViewChildType0Title;
        private TextView textViewChildType0Digest;
        private TextView textViewChildType0ReplyCount;

        public ChildViewHolderType0(View view) {
            networkImageViewChildType0 = (NetworkImageView) view.findViewById(R.id.child_type0_iv);
            textViewChildType0Title = (TextView) view.findViewById(R.id.child_type0_tv_title);
            textViewChildType0Digest = (TextView) view.findViewById(R.id.child_type0_tv_digest);
            textViewChildType0ReplyCount = (TextView) view.findViewById(R.id.child_type0_tv_replyCount);
        }
    }

    public class ChildViewHolderType1{
        private TextView textViewChildType1Title;
        private TextView textViewChildType1ReplyCount;
        private NetworkImageView networkImageViewChildType1First;
        private NetworkImageView networkImageViewChildType1Second;
        private NetworkImageView networkImageViewChildType1Third;

        public ChildViewHolderType1(View view) {
            textViewChildType1Title = (TextView) view.findViewById(R.id.child_type1_tv_title);
            textViewChildType1ReplyCount = (TextView) view.findViewById(R.id.child_type1_tv_replyCount);
            networkImageViewChildType1First = (NetworkImageView) view.findViewById(R.id.child_type1_iv_first);
            networkImageViewChildType1Second = (NetworkImageView) view.findViewById(R.id.child_type1_iv_second);
            networkImageViewChildType1Third = (NetworkImageView) view.findViewById(R.id.child_type1_iv_third);
        }
    }
//    private void imgJson(NetworkImageView networkImageView,  String url) {
//        RequestQueue mQueue = Volley.newRequestQueue(context);
//        ImageLoader imageLoader = new ImageLoader(mQueue,cache);
//        networkImageView.setDefaultImageResId(R.mipmap.iconfont_jiazaizhong);
//        networkImageView.setErrorImageResId(R.mipmap.iconfont_jiazaishibai);
//        networkImageView.setImageUrl(url,imageLoader);
//    }
}
